package drds.global_transaction.api.message_codec.request_and_response.transaction.global_transaction.branch_transaction_register;

import drds.global_transaction.api.api.RpcContext;
import drds.global_transaction.api.message_codec.Message;
import drds.global_transaction.api.message_codec.request_and_response.transaction.TransactionResponse;
import drds.global_transaction.api.message_codec.request_and_response.transaction.global_transaction.GlobalTransactionRequestMessage;
import lombok.Getter;
import lombok.Setter;

import java.nio.ByteBuffer;

public class BranchTransactionRegisterRequestMessage extends GlobalTransactionRequestMessage
{

	private static final long serialVersionUID = 1L;
	@Setter
	@Getter
	private long globalTransactionId;
	@Setter
	@Getter
	private String resourceId;
	@Setter
	@Getter
	private String tableNameAndLockRecordIdsStringsString;

	@Override
	public void decode(ByteBuffer byteBuffer)
	{
		this.globalTransactionId = byteBuffer.getLong();
		// this.branchType = BranchType.get(byteBuffer.get());
		short length = byteBuffer.getShort();
		if (length > 0)
		{
			byte[] bytes = new byte[length];
			byteBuffer.get(bytes);
			this.setResourceId(new String(bytes, Message.UTF8));
		}

		int $length = byteBuffer.getInt();
		if ($length > 0)
		{
			byte[] bytes = new byte[$length];
			byteBuffer.get(bytes);
			this.setTableNameAndLockRecordIdsStringsString(new String(bytes, Message.UTF8));
		}
	}

	@Override
	public short getTypeCode()
	{
		return Message.branch_transaction_register_request_message;
	}

	@Override
	public TransactionResponse handle(RpcContext rpcContext)
	{
		return transactionCoordinatorInboundHandler.handle(rpcContext, this);
	}

	@Override
	public ByteBuffer putBytesToByteBuffer()
	{
		ByteBuffer byteBuffer = createByteBuffer();
		byte[] tableNameAndLockRecordIdsStringsStringBytes = null;
		if (this.tableNameAndLockRecordIdsStringsString != null)
		{
			tableNameAndLockRecordIdsStringsStringBytes = tableNameAndLockRecordIdsStringsString.getBytes(Message.UTF8);
			if (tableNameAndLockRecordIdsStringsStringBytes.length > 512)
			{
				byteBuffer = ByteBuffer.allocate(tableNameAndLockRecordIdsStringsStringBytes.length + 1024);
			}
		}

		// 1. Transaction Id
		byteBuffer.putLong(this.globalTransactionId);
		// 2. Branch Type
//        byteBuffer.map$channelToRpcContextMap((byte) this.branchType.ordinal());
		// 3. Resource Id
		if (this.resourceId != null)
		{
			byte[] bytes = resourceId.getBytes(Message.UTF8);
			byteBuffer.putShort((short) bytes.length);
			if (bytes.length > 0)
			{
				byteBuffer.put(bytes);
			}
		} else
		{
			byteBuffer.putShort((short) 0);
		}

		// 4. Lock Key
		if (this.tableNameAndLockRecordIdsStringsString != null)
		{
			byteBuffer.putInt(tableNameAndLockRecordIdsStringsStringBytes.length);
			if (tableNameAndLockRecordIdsStringsStringBytes.length > 0)
			{
				byteBuffer.put(tableNameAndLockRecordIdsStringsStringBytes);
			}
		} else
		{
			byteBuffer.putInt(0);
		}
		markAsEncode();
		return byteBuffer;

	}
}
